//
//  DemoDateTime.m
//  ocdemo
//
//  Created by JIENING ZHANG on 2019/5/28.
//  Copyright © 2019 JIENING ZHANG. All rights reserved.
//

#import "DemoDateTime.h"
#import <stdarg.h>
#include <QuartzCore/CABase.h>

static CFTimeInterval startOfShowTS = 0;

void logTSBegin(NSString* log) {
#ifdef DEBUG
    startOfShowTS = CACurrentMediaTime();
    printf("logTS: 0ms, %s\n", log.UTF8String);
#endif
}

void logTS(NSString* logFmt, ...) {
#ifdef DEBUG
    
    CFTimeInterval dt = CACurrentMediaTime() - startOfShowTS;
    char *tInfo = NSThread.isMainThread ? "MT" : "GQ";
    printf ("logTS: %dms, %s, ",(int)(dt*1000), tInfo);
    
    // 不定参数
    va_list args;
    va_start(args, logFmt);
    NSString *s = [[NSString alloc] initWithFormat:logFmt arguments:args];
    va_end(args);
    
    
    // 不定参数
    //    va_list args;
    //    va_start(args, logFmt);
    //    //vprintf(logFmt.UTF8String, args);
    //    NSLogv(logFmt, args);
    //    va_end(args);
    
    printf("%s\n", s.UTF8String);
#endif
}

@implementation DemoDateTime
+ (void)timeStamp {
    
    NSDate *dateNow = [NSDate date];
    
    // 日期对象到时间戳
    //   秒时间戳，浮点
    NSTimeInterval dateNowTSDoube = [dateNow timeIntervalSince1970];
    //   秒时间戳，长整
    long long dateNowTSSeconds = dateNowTSDoube;
    //   毫秒时间戳，长整
    long long dateNowTSlMilliseconds = dateNowTSDoube*1000 ;
    //   微秒时间戳，长整
    long long dateNowTSMicrosecond = dateNowTSDoube*1000000 ;
    
    NSLog(@"date2ts: date(%@) --> ts: %.6f", dateNow, dateNowTSDoube);
    NSLog(@"date2ts: date(%@) --> ts: %lld(s)", dateNow, dateNowTSSeconds);
    NSLog(@"date2ts: date(%@) --> ts: %lld(ms)", dateNow, dateNowTSlMilliseconds);
    NSLog(@"date2ts: date(%@) --> ts: %lld(μs)", dateNow, dateNowTSMicrosecond);
    
    // 昨日此秒
    NSTimeInterval yesterdayTSDouble =  dateNowTSDoube - 86400.0;
    long long yesterdayTSSeconds =          yesterdayTSDouble;
    long long yesterdayTSlMilliseconds =    yesterdayTSDouble*1000;
    long long yesterdayTSMicrosecond =      yesterdayTSDouble*1000000 ;
    
    NSDate *dateYesterday  = [NSDate dateWithTimeIntervalSince1970:yesterdayTSDouble];
    
    NSLog(@"date2ts: date(%@) --> ts: %.6f", dateYesterday, yesterdayTSDouble);
    NSLog(@"date2ts: date(%@) --> ts: %lld(s)", dateYesterday, yesterdayTSSeconds);
    NSLog(@"date2ts: date(%@) --> ts: %lld(ms)", dateYesterday, yesterdayTSlMilliseconds);
    NSLog(@"date2ts: date(%@) --> ts: %lld(μs)", dateYesterday, yesterdayTSMicrosecond);
    
    // 字符串到日期对象
    NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
    df1.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *dateStr1 = @"2019-05-05 23:32:59";
    NSDate *date1 = [df1 dateFromString:dateStr1];
    
    NSTimeInterval date1TS = [date1 timeIntervalSince1970];
    NSLog(@"date2ts: date(%@) --> %.6f", date1, date1TS);
}

+ (void)demoLogic {
    
    // 计算耗时单位毫秒
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    [DemoDateTime timeStamp];
    
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    
    NSLog(@"[DemoDateTime timeStamp] call time cost : %f(ms) ", end - start);
}
@end
